$(function () {
    seleltDepartment();
});

// 查询所有部门
function selectAllDep(){
    $.ajax({
        url: "/selectAllDep",
        type: "POST",
        dataType: "json",
        timeout: 1000,
        cache: false,
        success: function(department) {
            alert(department);
            console.log(department);
            if (department.success){
                alert(department);
                var departmentlist = department.data.valueOf();
                alert(departmentlist);
                for (var i = 0; i < departmentlist.length; i++){
                    var departmentAll = departmentlist[i];
                    $("#staff_department").append(
                        "<option value="+departmentAll.depid+">"+departmentAll.depname+"</option>"
                    );
                }
            }else{
                $("#staff_department").append(
                    "<option value=''>读取失败</option>"
                );
            }
        }
    })
}

// 查询部门,用于员工的根据部门查询对应的职位
function seleltDepartment(){
    $.ajax({
        url: "/selectAllDep",
        type: "POST",
        dataType: "json",
        timeout: 1000,
        cache: false,
        success: function(department) {
            console.log(department);
            if (department.success){
                var departmentlist = department.data.valueOf();
                for (var i = 0; i < departmentlist.length; i++){
                    var departmentAll = departmentlist[i];
                    $("#staff_department").append(
                        "<option value="+departmentAll.depid+">"+departmentAll.depname+"</option>"
                    );
                    depid = departmentAll.depid;
                }
            }else{
                $("#staff_department").append(
                    "<option value=''>读取失败</option>"
                );
            }
        }
    })
}
$("#staff_department").change(function () {
    var id = $("#staff_department").val();  // 获取被选中的数据的编号
    var a = $("#staff_department option:checked").text();   // 获取被选中的值的默认值
    $.ajax({
        url: "/selectDepPosition",
        type: "POST",
        data:{department_id:id},
        dataType: "json",
        timeout: 1000,
        cache: false,
        success: function(department) {
            console.log(department);
            if (department.success){
                $("#staff_position").empty();
                var departmentlist = department.data.valueOf();
                if (departmentlist.length <= 0){
                    $("#staff_position").append(
                        "<option>该部门暂时无职位</option>"
                    );
                    $("#add_staff").attr("disabled",true);
                    $("#upl_staff").attr("disabled",true);
                }else{
                    $("#add_staff").attr("disabled",false);
                    $("#upl_staff").attr("disabled",false);
                    for (var i = 0; i < departmentlist.length; i++ ){
                        var departmentAll = departmentlist[i];
                        $("#staff_position").append(
                            "<option value="+departmentAll.posid+">"+departmentAll.posname+"</option>"
                        );
                    }
                }
            }else{
                $("#staff_position").append(
                    "<option value=''>读取失败</option>"
                );
            }
        }
    })
})
